package com.xoa.util.cms.directive;

import com.xoa.util.DateFormat;
import com.xoa.util.common.StringUtils;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

import java.io.IOException;
import java.io.Writer;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.zip.DataFormatException;

public class CMSOutDirective  implements TemplateDirectiveModel{
    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        String name =  StringUtils.getString(map.get("name"));
        String dataFormate = StringUtils.getString(map.get("dataFormate"));

        Map loopObj = (Map) environment.getCustomAttribute("loopObj");
        Writer w = environment.getOut();

        //上层变量不为空，输出name对应的值
        if(loopObj != null){
           if("".equals(dataFormate)){
               //如果dataFormate为空说明不需要格式化
               w.write(loopObj.get(name)+"");
           }else{
               try {
                   Object o = loopObj.get(name);
                   //判断o是否是一个时间格式
                   if(o instanceof Date || o instanceof  Calendar){
                       w.write(DateFormat.getFormatByUse(dataFormate,o));
                   }
               } catch (DataFormatException e) {
                   e.printStackTrace();
               }
           }
        }

        if(templateDirectiveBody != null){
            templateDirectiveBody.render(w);
        }
    }
}
